package com.funmi.lizip.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.funmi.lizip.common.base.R;
import com.funmi.lizip.entity.PageParam;
import com.funmi.lizip.entity.vo.ImageVo;
import com.funmi.lizip.service.DeviceService;
import com.funmi.lizip.service.ImageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api(tags = "有害生物防治模块")
@RestController
@RequestMapping("/image")
@Slf4j
public class ImageController {

    @Autowired
    private ImageService imageService;

    @Autowired
    private DeviceService deviceService;


    /**
     * 3.同比: 2022年8月的设备数和拍摄数和2021年8月的设备数和拍摄数比较
     * 环比:2022.11月和2022.10月的的设备数和拍摄数
     */
    @ApiOperation(value = "同比设备数和拍摄数|环比设备数和拍摄数")
    @PostMapping("/RingRatioYOverYNum")
    public R RingRatioYOverYNum(@RequestBody() HashMap<String, Object> stringObjectHashMap) {
        try {
            String s = JSONObject.toJSONString(stringObjectHashMap);
            HashMap hashMap = JSON.parseObject(s, HashMap.class);
            Object date = hashMap.get("DateYM");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
            Date parse = simpleDateFormat.parse(date.toString());

            List<Map<String, Object>> mapList = imageService.RingRatioYOverYNum(parse);
            return R.success().data(mapList);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }


    }


    /**
     * 4.本月的的设备数和拍摄数相比上个月上涨了多少百分点 相比上年上涨了多少个百分点
     */
    @ApiOperation(value = "上涨的百分点")
    @PostMapping("/IncTreaseByPercentagePoints")
    public R increaseByPercentagePoints(@RequestBody HashMap<String, Object> stringObjectHashMap) {
        try {
            String s = JSONObject.toJSONString(stringObjectHashMap);
            HashMap hashMap = JSON.parseObject(s, HashMap.class);
            Object date = hashMap.get("DateYM");

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
            Date parse = simpleDateFormat.parse(date.toString());

            Map<String, Object> maps = imageService.increaseByPercentagePoints(parse);
            return R.success().data(maps);
        } catch (NullPointerException e) {
            e.printStackTrace();
           return R.fail().message("参数错误");
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }


    }

//    @ApiOperation(value = "根据图片进行分页")
//    @GetMapping("")
//    public R pageList(ImageQueryRequest request) {
//        long current = 1;
//        long size = 10;
//
//        Image imageQuery = new Image();
//        if (request != null) {
//            BeanUtils.copyProperties(request, imageQuery);
//            current = request.getPage();
//            size = request.getPageSize();
//        }
//
//        QueryWrapper<Image> qw = new QueryWrapper<>(imageQuery);
//
//        List<String> type = deviceService.getType(imageQuery.getDeviceId());
//
//        if (request != null && type != null) {
//            // qw.eq("type", type);
//            qw.in("type",type);
//        }
//
//        if (request != null && request.getImei() != null) {
//            qw.eq("imei", imageQuery.getImei());
//        }
//
//
//        Page<Image> imagePage = imageService.page(new Page<>(current, size), qw);
//        PageDTO<ImageVo> imageVoPage = new PageDTO<>(imagePage.getCurrent(), imagePage.getSize(), imagePage.getTotal());
//
//        List<ImageVo> imageVoList = imagePage.getRecords().stream().map(image -> {
//            ImageVo imageVo = new ImageVo();
//            BeanUtils.copyProperties(image, imageVo);
//            return imageVo;
//        }).collect(Collectors.toList());
//
//        imageVoPage.setRecords(imageVoList);
//        return R.success().data(imageVoPage);
//
//
//    }

    //[ { optionNam: '123', id: 1}]
    @ApiOperation(value = "根据图片进行分页")
    @GetMapping("/pageImage")
    public R pageList(PageParam<ImageVo> pageParam, String type, String imei) {
        IPage<ImageVo> IPage = imageService.pageList(pageParam, type, imei);


        return R.success().data(IPage);
    }
}
